In computing, late binding or dynamic linkage—though not an identical process to Dynamic linker imported code libraries—is a computer programming mechanism in which the method being called upon an object, or the Subroutine being called with arguments, is looked up by name at runtime. In other words, a name is associated with a particular operation or object at runtime, rather than during Compiler. The name dynamic binding is sometimes used,Booch, Grady. Object-oriented Analysis and Design. Addison-Wesley, 1994. p71 but is more commonly used to refer to dynamic scope.
With early binding, or static binding, in an object-oriented language, the compilation phase fixes all Data type of variables and expressions. This is usually stored in the compiled program as an offset in a virtual method table ("v-table"). In contrast, with late binding, the compiler does not read enough information to verify the method exists or bind its slot on the v-table. Instead, the method is looked up by name at runtime.
The primary advantage of using late binding in Component Object Model (COM) programming is that it does not require the compiler to reference the libraries that contain the object at compile time. This makes the compilation process more resistant to version conflicts, in which the class's v-table may be accidentally modified. (This is not a concern in just-in-time compiled platforms such as .NET or Java, because the v-table is created at runtime by the virtual machine against the libraries as they are being loaded into the running application.)
In the 1980s Smalltalk popularized object-oriented programming (OOP) and with it late binding. Alan Kay once said, "OOP to me means only messaging, local retention, and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."
In the early to mid-1990s, Microsoft heavily promoted its COM standard as a binary interface between different OOP programming languages. COM programming equally promoted early and late binding, with many languages supporting both at the syntax level.
In 2000, Alex Martelli coined the term "duck typing" to refer to a similar concept, but with a different emphasis. While late binding generally focuses on implementation details, duck typing focuses on the ability to ignore types and concentrate on the methods an object currently has.
Example using an interactive Clozure CL session:
? (defun bar (x) ; now we define it
? (foo) ; calling foo and it uses the recent definition of BAR
6.283185307179586D0
? (defun bar (x) ; now we redefine BAR
? (foo) ; FOO now calls the new function, there is no need to recompile/link/load FOO
3141.592653589793D0
? (type-of 'bar) ; BAR is a symbol
SYMBOL
? (symbol-function 'bar) ; the symbol BAR has a function binding
(bar pi)) ; a still undefined function BAR gets called
FOO
(* x 2))
BAR
(* x 1000))
BAR
Also like COM and Java, the Common Language Runtime provides reflection APIs that can make late binding calls. The use of these calls varies by language.
With C# 4, the language also added the "dynamic" pseudo-type. This would be used in place of the Object type to indicate that late binding is desired. The specific late binding mechanism needed is determined at runtime using the Dynamic Language Runtime as a starting point.
Visual Basic uses them whenever the variable is of type Object and the compiler directive "Option Strict Off" is in force. This is the default setting for a new VB project. Prior to version 9, only .NET and COM objects could be late bound. With VB 10, this has been extended to DLR-based objects.
Early documents on Java discussed how classes were not linked together at compile time. While types are statically checked at compile time, different implementations for classes could be swapped out just prior to runtime simply by overwriting the class file. As long as the new class definition had the same class and method names, the code would still work. In this sense it is similar to the traditional definition of late binding.
Currently, it is popular to use the term late binding in Java programming as a synonym for dynamic dispatch. Specifically, this refers to Java's single dispatch mechanism used with virtual methods.
Finally, Java can use late binding using its reflection APIs and type introspection much in the same way it is done in COM and .NET programming. Generally speaking those who only program in Java do not call this late binding. Likewise the use of "duck typing" techniques is frowned upon in Java programming, with abstract interfaces used instead.
Oracle, the current owner of Java, has been known to use the term late binding in the "duck typing" sense when discussing both Java and other languages in the same documentation.
When using late binding the timestamp check is not performed, and the stored procedure is executed via an anonymous PL/SQL block. While this can be slower, it removes the need to recompile all of the client applications when a stored procedure changes.
This distinction appears to be unique to PL/SQL and Ada. Other languages that can call PL/SQL procedures, as well as other database engines, only use late binding.
For some compilers, late binding may prevent the use of static type checking. When making a late bound call, the compiler has to assume that the method exists. This means a simple spelling error can cause a run-time error to be thrown. Modern compilers avoid this by ensuring that every possible call must have an implementation during compilation.
Late binding may prevent forms of static analysis needed by an integrated development environment (IDE). For example, an IDE's "go to definition" feature may not function on a late-bound call, if the IDE has no way to know which class the call may refer to. A modern IDE easily solves this especially for object-oriented languages since a late-bound method always specifies an interface or base class, which is where "go to definition" leads, and "find all references" can be used to find all implementations or overrides.
|
|